//
//  CSLoadController.m
//  CreateWithSuper
//
//  Created by chenjunbo on 2024/6/7.
//  Copyright © 2024 CS. All rights reserved.
//

#import "CSLoadController.h"
#import "CSTutorialController.h"
#import "CSCreateController.h"
#import "ADModule.h"

@interface CSLoadController ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *widCst;

// 进度条外层
@property (weak, nonatomic) IBOutlet UIView *scheduleView;

@property (nonatomic, strong) NSTimer *scheduleTimer;

@end

@implementation CSLoadController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [UIView animateWithDuration:10 animations:^{
        self.widCst.constant = self.scheduleView.frame.size.width;
        [self.view layoutIfNeeded];
    }];
    
    __block int timeCount = 0;
    
    [[ADModule standardInstance]requestAllAd];
    
    self.scheduleTimer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        
        timeCount ++;
        
        if (timeCount > 3 && timeCount < 12){

            if ([[ADModule standardInstance]haveOpenAd]){
                [self.scheduleTimer invalidate];
                self.scheduleTimer = nil;
                
                [ADModule standardInstance].openDismiss = ^{
                    [self push];
                };
                
                [[ADModule standardInstance]showOpenAd];

            }
            
            
        }else if (timeCount >= 12){
            [self.scheduleTimer invalidate];
            self.scheduleTimer = nil;
            

            [self push];
            

        }
        
        
    }];
    
    [[NSRunLoop currentRunLoop]addTimer:self.scheduleTimer forMode:NSRunLoopCommonModes];
    
}

- (void)push{
    
    if (self.needPop){
        [self.navigationController popViewControllerAnimated:NO];
    }else{
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Create" bundle:nil];
        CSCreateController *vc = [storyboard instantiateViewControllerWithIdentifier:@"CSCreateController"];
        
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
        
        [UIApplication sharedApplication].keyWindow.rootViewController = nav;
//
//        if (![[NSUserDefaults standardUserDefaults]boolForKey:@"ShowedCSTutorialController"]){
//            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Create" bundle:nil];
//            CSTutorialController *vc = [storyboard instantiateViewControllerWithIdentifier:@"CSTutorialController"];
//            [UIApplication sharedApplication].keyWindow.rootViewController = vc;
//        }else{
//            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Create" bundle:nil];
//            CSCreateController *vc = [storyboard instantiateViewControllerWithIdentifier:@"CSCreateController"];
//            
//            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
//            
//            [UIApplication sharedApplication].keyWindow.rootViewController = nav;
//        }
        
    }


}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
